<template>
  <div class="register-container">
    <!-- 注册内容 -->
    <div class="register">
      <h3>
        注册新用户
        <span class="go">我有账号，去 <a href="../login" target="_blank">登录</a> </span>
      </h3>

      <el-form label-suffix=":" label-width="120px" :model="userParams" :rules="rules" ref="formRef">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="userParams.phone" placeholder="请输入你的手机号" type="text" />
        </el-form-item>

        <el-form-item label="验证码" prop="code">
          <el-input v-model="userParams.code" type="text" placeholder="请输入验证码" />
          <el-button type="info" @click="getCode">获取验证码</el-button>
        </el-form-item>

        <el-form-item label="登录密码" prop="password">
          <el-input v-model="userParams.password" type="text" placeholder="请输入你的登录密码" />
        </el-form-item>

        <el-form-item label="确认密码" prop="verifyPwd">
          <el-input v-model="userParams.verifyPwd" type="text" placeholder="请输入确认密码" />
        </el-form-item>

        <el-form-item prop="select">
          <el-checkbox v-model="userParams.select">同意协议并注册《尚品汇用户协议》</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button :plain="true" @click="register" class="Sign" type="danger">完成注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reqRegister, reqSendCode } from '@/api/register'
import type { IRegisterParams } from '@/types/register'

import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'

const userParams = ref<IRegisterParams>({
  phone: '',
  code: '',
  password: '',
  verifyPwd: '',
  select: false
})

const router = useRouter()

// 兜底验证引用
const formRef = ref<FormInstance>()

// 获取验证码
const getCode = async () => {
  const res = await reqSendCode(userParams.value.phone)
  // 赋值
  // console.log(res)
  userParams.value.code = res.data as string
}
// 确认密码验证
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请重新输入密码'))
  } else if (value !== userParams.value.password) {
    callback(new Error('两次密码不匹配!'))
  } else {
    callback()
  }
}
const rules = reactive<FormRules>({
  phone: [
    { required: true, message: '请输入你的手机号', trigger: 'blur' },
    {
      pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
      message: '手机号码不正确',
      trigger: 'blur'
    }
  ],
  code: [
    { required: true, message: '请获取验证码', trigger: 'change' },
    { pattern: /^\d{6}$/, message: '验证码有误', trigger: 'change' }
  ],
  password: [
    { required: true, message: '请输入你的登录密码', trigger: 'blur' },
    { pattern: /^\w{6,21}$/, message: '登录密码有误', trigger: 'blur' }
  ],

  verifyPwd: [
    { required: true, message: '请输入确认密码', trigger: 'blur' },
    { pattern: /^\w{6,21}$/, validator: validatePass, trigger: 'blur' }
  ],
  select: [{ required: true, message: '是否同意协议', trigger: 'change' }]
})

// 注册完成
const register = async () => {
  const { phone, code, password } = userParams.value

  // 兜底验证
  await formRef.value?.validate()

  // 收集参数
  const params = {
    phone,
    code,
    password
  }
  console.log(params)
  const data = await reqRegister(params)
  console.log(data)
  if (data.code === 200) {
    ElMessage.success('注册成功')
    router.replace('/login')
  }
}
</script>

<script lang="ts">
export default {
  name: 'RegisterView'
}
</script>

<style lang="scss" scoped>
.register-container {
  .register {
    width: 1200px;
    height: 445px;
    border: 1px solid rgb(223, 223, 223);
    margin: 0 auto;

    h3 {
      background: #ececec;
      margin: 0;
      padding: 6px 15px;
      color: #333;
      border-bottom: 1px solid #dfdfdf;
      font-size: 20.04px;
      line-height: 30.06px;

      span {
        font-size: 14px;
        float: right;

        a {
          color: #e1251b;
        }
      }
    }
  }
}

.register-container .register div[data-v-c5d9c666]:nth-of-type(1) {
  margin-top: 0px;
}

.el-form {
  width: 700px;
  height: 500px;
  padding: 50px;
  margin: 30px auto;

  .el-input {
    width: 400px;
  }
  .el-button {
    margin-left: 10px;
  }
  .Sign {
    width: 400px;
  }
}
</style>
